package com.zj.leetcode.easy.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description: N 叉树的后序遍历
 * 590 n-ary-tree-postorder-traversal
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class NAryTreePostorderTraversal {
    public static void main(String[] args) {
        Solution solution = new NAryTreePostorderTraversal().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    ;


    class Solution {
        List<Integer> list = new ArrayList<>();

        public List<Integer> postorder(Node root) {
            if (root == null) {
                return list;
            }
            this.dfs(root);
            return list;

        }


        public void dfs(Node node) {
            if (node != null) {
                if (node.children != null
                        && node.children.size() != 0) {
                    for (Node child : node.children) {
                        this.dfs(child);
                        System.out.println(child.val);
                    }
                }
                list.add(node.val);
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
